{layout name="public/layout" /}
{include file='yxnavbar'}


<style type="text/css">
    .tuan_div{
        height: 36px;
    }
    .tuan_div300{
        height: 200px;
    }
    .tuan_title{
        width: 140px;
        text-align: right;
        font-size: 14px;
        color: #606266;
        font-weight: 700;
        padding-right: 12px;
    }
    .right_btn{
        display: inline-block;
        float: right;
        border: 1px solid #ff4949;
        padding: 4px 8px;
        color: #ff4949;
        font-size: 12px;
        background: #ffeded;
        border-radius: 3px;
    }
    .ml100{
        margin-left: 100px !important;
    }
    .right_btn_blue{
        color: #ffffff;
        background-color: #1890ff;
        border-color: #1890ff;
    }
    .layui-layer-shade{
        z-index: 1 !important;
    }
    .layui-layer-page{
        z-index: 2 !important;
    }
    .minuteselect,.hourselect{
        min-width: 80px;
    }
</style>
<div class="main">
    {include file='../../plugin/beilai/template/web/common_nav'}
    <div class="panel panel-default" role="tabpanel">
        <div class="panel-heading">商家分类
            <div class="right_btn right_btn_blue" id="open_btn_coupon">增加一级分类</div></div>
        <div class="panel-body">
            <table class="table table-hover">
                <thead class="navbar-inner">
                <tr style="">
                    <th style="width:50px;">序号</th>
                    <th style="width:120px;">分类名称</th>
                    <th style="width:120px;">排序</th>
                    <th style="width:120px;">可选商家数</th>
                    <th style="width:100px; text-align:right;">操作</th>
                </tr>
                </thead>
                <tbody>
                {loop $first $i $item}
                <tr class="str_center" {if $item['pid']!=0}style="background-color:#cdedf6"{/if}>
                    <td class="str_center">{php echo $i+1+($pageNumber - 1) * $pageSize;}</td>
                    <td class="str_center">{if $item['pid']!=0}<i class="fa fa-terminal" aria-hidden="true"></i>{/if}{$item['ct_name']}</td>
                    <td class="str_center">{$item['ct_sort']}</td>
                    <td class="str_center">{$item['ct_sj_min']}/{$item['ct_sj']}</td>
                    <td class="str_center" style="text-align:right;">
                        {if $item['pid']==0}
                        <a onclick="changeOrDelItem(this)"
                           data-edit-url="{php echo createWebUrlPlugin('beilai.category.edit_child',array('themeid'=>$themeid));}" data-action="add_child"
                           title="添加二级分类" style="width: 30px" class="btn btn-success btn-sm"  data-toggle="tooltip" data-placement="top"><i class="fa fa-arrows"> </i>
                        </a>
                        {/if}

                        {if $item['pid']==0}
                        <a onclick="changeOrDelItem(this)"
                           data-edit-url="{php echo createWebUrlPlugin('beilai.category.getItem',array('themeid'=>$themeid,'ct_id'=>$item['ct_id'],'op'=>'edit'));}" data-action="edit"
                           title="修改" style="width: 30px" class="btn btn-info btn-sm"  data-toggle="tooltip" data-placement="top"><i class="fa fa-pencil-square-o"> </i>
                        </a>
                        {else}
                        <a onclick="changeOrDelItem(this)"
                           data-edit-url="{php echo createWebUrlPlugin('beilai.category.edit_child',array('themeid'=>$themeid,'ct_id'=>$item['ct_id'],'op'=>'edit_child'));}" data-action="edit_child"
                           title="修改" style="width: 30px" class="btn btn-info btn-sm"  data-toggle="tooltip" data-placement="top"><i class="fa fa-pencil-square-o"> </i>
                        </a>
                        {/if}
                        <a onclick="changeOrDelItem(this)"
                           data-edit-url="{php echo createWebUrlPlugin('beilai.category.del',array('themeid'=>$themeid,'ct_id'=>$item['ct_id'],'op'=>'del'));}" data-action="del"
                           title="删除" style="width: 30px" class="btn btn-danger btn-sm"  data-toggle="tooltip" data-placement="top"><i class="fa fa-times"> </i>
                        </a>
                    </td>
                </tr>
                {/loop}
                </tbody>

            </table>
            {$pager}
        </div>
    </div>
    <!--    添加一级分类-->
    <form action="" method="post" class="form-horizontal" role="form" name="show_form1" style="display:none;width: 600px;margin-top:20px;" id="show_form1">
        <input type="hidden" name="themeid" value="{$themeid}"/>
        <input type="hidden" name="ct_id" id="ct_id" value=""/>
        <div class="form-group ml100">
            <label class="col-xs-12 col-sm-4 col-md-3 col-lg-3 control-label"><span style="color: red;">*</span>排序</label>
            <div class="col-sm-8 col-xs-12">
                <input type="number" class="form-control"  placeholder="请输入排序" name="ct_sort"  id="ct_sort" value="">
            </div>
        </div>
        <div class="form-group ml100">
            <label class="col-xs-12 col-sm-4 col-md-3 col-lg-3 control-label"><span style="color: red;">*</span>分类名称</label>
            <div class="col-sm-8 col-xs-12">
                <input type="text" class="form-control"  placeholder="请输入分类名称" name="ct_name"  id="ct_name" value="">
            </div>
        </div>
        <div class="form-group ml100">
            <label class="col-xs-12 col-sm-4 col-md-3 col-lg-3 control-label"><span style="color: red;">*</span>最大可选商家数量</label>
            <div class="col-sm-8 col-xs-12">
                <input type="number" class="form-control"  placeholder="请输入最大可选商家数量" name="ct_sj"  id="ct_sj" value="0">
            </div>
        </div>
        <div class="form-group ml100">
            <label class="col-xs-12 col-sm-4 col-md-3 col-lg-3 control-label"><span style="color: red;">*</span>最小可选商家数量</label>
            <div class="col-sm-8 col-xs-12">
                <input type="number" class="form-control"  placeholder="请输入最小可选商家数量" name="ct_sj_min"  id="ct_sj_min" value="0">
            </div>
        </div>

        <div class="form-group ml100"  >
            <div class="col-sm-12">
                <input name="submit" onclick="addOrEditItem(1)"  type="button" value="提交" class="btn btn-primary col-lg-2">
            </div>
        </div>
    </form>
    <!--    添加一级分类-->
<!--    添加二级分类-->
    <form action="" method="post" class="form-horizontal" role="form" name="show_form2" style="display:none;width: 600px;margin-top:20px;" id="show_form2">
        <input type="hidden" name="themeid" value="{$themeid}"/>
        <input type="hidden" name="ct_id" id="ct_id1" value=""/>
        <div class="form-group ml100">
            <label class="col-xs-12 col-sm-4 col-md-3 col-lg-3 control-label"><span style="color: red;">*</span>排序</label>
            <div class="col-sm-8 col-xs-12">
                <input type="number" class="form-control"  placeholder="请输入排序" name="ct_sort"  id="ct_sort1" value="">
            </div>
        </div>
        <div class="form-group ml100">
            <label class="col-xs-12 col-sm-4 col-md-3 col-lg-3 control-label"><span style="color: red;">*</span>分类名称</label>
            <div class="col-sm-8 col-xs-12">
                <input type="text" class="form-control"  placeholder="请输入分类名称" name="ct_name"  id="ct_name1" value="">
            </div>
        </div>


        <div class="form-group ml100">
            <label class="col-xs-12 col-sm-4 col-md-3 col-lg-3 control-label"><span style="color: red;">*</span>最大可选商家数量</label>
            <div class="col-sm-8 col-xs-12">
                <input type="number" class="form-control"  placeholder="请输入最大可选商家数量" name="ct_sj"  id="ct_sj1" value="0">
            </div>
        </div>
        <div class="form-group ml100">
            <label class="col-xs-12 col-sm-4 col-md-3 col-lg-3 control-label"><span style="color: red;">*</span>最小可选商家数量</label>
            <div class="col-sm-8 col-xs-12">
                <input type="number" class="form-control"  placeholder="请输入最小可选商家数量" name="ct_sj_min"  id="ct_sj_min1" value="0">
            </div>
        </div>

        <div class="form-group ml100">
            <label class="col-xs-12 col-sm-4 col-md-4 col-lg-3 control-label"><span style="color: red;">*</span>选择归属一级分类</label>
            <div class="col-sm-8 col-xs-12">
                <select class="form-control" name="pid" id="pid1">
                </select>
            </div>
        </div>
        <div class="form-group ml100"  >
            <div class="col-sm-12">
                <input name="submit" onclick="addOrEditItem(2)"  type="button" value="提交" class="btn btn-primary col-lg-2">
            </div>
        </div>

    </form>
<!--    添加二级分类-->
</div>


<script type="text/javascript">
    //新增\修改奖品 提交数据
    function addOrEditItem(no) {
        let postUrl = "{php echo createWebUrlPlugin('beilai.category.edit',array('themeid'=>$themeid));}";
        if (no==1){
            postUrl = "{php echo createWebUrlPlugin('beilai.category.edit',array('themeid'=>$themeid));}";
        }else if(no==2){
            postUrl = "{php echo createWebUrlPlugin('beilai.category.edit',array('themeid'=>$themeid));}";
        }else if (no==3){
            postUrl = "{php echo createWebUrlPlugin('beilai.setting.edit',array('themeid'=>$themeid,'op'=>'btm_img_setting'));}";
        }else if (no==4){
            postUrl = "{php echo createWebUrlPlugin('beilai.setting.edit',array('themeid'=>$themeid,'op'=>'worker_money_setting'));}";
        }else if (no==5){
            postUrl = "{php echo createWebUrlPlugin('beilai.setting.edit',array('themeid'=>$themeid,'op'=>'common_setting'));}";
        }

        let success_redirect = "{php echo createWebUrlPlugin('beilai.category',array('themeid'=>$themeid));}";
        var from_no = "#show_form"+no;
        let formData = $(from_no).serialize();
        $.ajax({
            url:postUrl,
            dataType:'json',
            method:'post',
            data:formData,
            success:function (e) {
                console.log(e)
                let status = e.status;
                let result = e.result;
                if (status!=1){
                    layer.msg(result.msg)
                }else{
                    layer.msg(result.msg);
                    window.location.href = success_redirect;
                }
            },
            fail:function (err) {
                console.log(err)
            }
        })
    }

    $("#open_btn_first").on("click",function () {
        var index = layer.open({
            type:1,
            title:['新增解锁奖','font-size:18px;'],
            area:'700px',
            content:$('#show_form')
        })
    });
    $("#open_btn_coupon").on("click",function () {
        var index = layer.open({
            type:1,
            title:['添加一级分类','font-size:18px;'],
            area:'700px',
            content:$('#show_form1')
        })
    });
    //添加修改一级分类
    function addOrEditCoupon(){
        let postUrl = "{php echo createWebUrlPlugin('beilai.setting.edit',array('themeid'=>$themeid,'op'=>'add_edit_coupon'));}";
        let success_redirect = "{php echo createWebUrlPlugin('beilai.setting',array('themeid'=>$themeid));}";
        let formData = $("#show_form1").serialize();
        $.ajax({
            url:postUrl,
            dataType:'json',
            method:'post',
            data:formData,
            success:function (e) {
                let status = e.status;
                let result = e.result;
                if (status!=1){
                    layer.msg(result.msg)
                }else{
                    layer.msg(result.msg);
                    window.location.href = success_redirect;
                }
            },
            fail:function (err) {
                console.log(err)
            }
        })
    }
    //修改删除分类
    function changeOrDelItem(e){
        let postUrl =e.getAttribute("data-edit-url");
        let action = e.getAttribute("data-action");
        if (action=='del'){
            layer.msg('您确定删除此分类吗？',{
                btn:['确定','取消'],
                yes:function (index) {
                    $.ajax({
                        url:postUrl,
                        dataType:'json',
                        method:'post',
                        data: {},
                        success:function (e) {
                            let res = e.result;
                            let status = res.status;
                            let msg = res.msg;
                            if (status!=1){
                                layer.msg(msg);
                            }else{
                                layer.msg(msg);
                            }
                            window.parent.location.reload();
                        },
                        fail:function (err) {
                            console.log(err)
                        }
                    });
                }
            });
        }else if(action=='add_child'){
            $.ajax({
                url:postUrl,
                dataType:'json',
                method:'post',
                data: {},
                success:function (e) {
                    let res = e.result;
                    let data = res.data;
                    let status = e.status;
                    let msg = res.msg;
                    let item = data.item;
                    $("#ct_id1").val(item.ct_id);
                    $("#ct_sort1").val(item.ct_sort);
                    $("#ct_name1").val(item.ct_name);
                    $("#ct_sj1").val(0);
                    $("#ct_sj_min1").val(0);
                    $("#pid1").html(data.list);

                    var index = layer.open({
                        type:1,
                        title:['添加二级分类','font-size:18px;'],
                        area:'700px',
                        content:$('#show_form2')
                    })
                },
                fail:function (err) {
                    console.log(err)
                }
            });
        }else if(action=='edit_child'){
            $.ajax({
                url:postUrl,
                dataType:'json',
                method:'post',
                data: {},
                success:function (e) {
                    let res = e.result;
                    let data = res.data;
                    let status = e.status;
                    let msg = res.msg;
                    let item = data.item;
                    $("#ct_id1").val(item.ct_id);
                    $("#ct_sort1").val(item.ct_sort);
                    $("#ct_name1").val(item.ct_name);
                    $("#ct_sj1").val(item.ct_sj);
                    $("#ct_sj_min1").val(item.ct_sj_min);
                    $("#pid1").html(data.list);

                    var index = layer.open({
                        type:1,
                        title:['修改二级分类','font-size:18px;'],
                        area:'700px',
                        content:$('#show_form2')
                    })
                },
                fail:function (err) {
                    console.log(err)
                }
            });
        }else{
            $.ajax({
                url:postUrl,
                dataType:'json',
                method:'post',
                data: {},
                success:function (e) {
                    let res = e.result;
                    let data = res.data;
                    let status = e.status;
                    let msg = res.msg;
                    if (status!=1){
                        layer.msg(msg);
                    }else{
                        console.log(data)
                        $("#ct_id").val(data.ct_id);
                        $("#ct_sort").val(data.ct_sort);
                        $("#ct_name").val(data.ct_name);
                        $("#ct_sj").val(data.ct_sj);
                        $("#ct_sj_min").val(data.ct_sj_min);
                        var index = layer.open({
                            type:1,
                            title:['修改分类','font-size:18px;'],
                            area:'700px',
                            content:$('#show_form1')
                        })
                    }
                },
                fail:function (err) {
                    console.log(err)
                }
            });
        }
    }

</script>
